package com.tygz.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auto.base.pojo.SysParameter;
import com.auto.base.pojo.SysUser;
import com.auto.base.service.SysParameterService;
import com.auto.common.BaseController;
import com.auto.util.SystemUtil;
import com.base.common.CommonConstants;
import com.base.common.RequestParameters;
import com.base.util.JUtil;



@Controller
@RequestMapping("/sys/SysParameterExt")
public class SysParameterExtController extends BaseController {

	private final Logger log = LoggerFactory.getLogger(SysParameterExtController.class);
  @Resource
    public  SysParameterService service;



	/**
	 * 新增和修改
	 * @param record
	 * @return
	 */
	@RequestMapping(value = "addOrUpdate")
	@ResponseBody
	public Object addOrUpdate( @ModelAttribute SysParameter  record,HttpServletRequest request) {
		try {
			if(null == record){
				return setFail("接收数据为空！");
			}
			SysUser loginedUser = getLoginSysUser(request);
			String key = record.getParameterKey(),value = record.getParameterValue();
			if(JUtil.isNULL(record.getId())) {
				record.setAddTime(new Date().getTime());
				record.setAddUser(loginedUser.getId());
				record.setUpdateTime(record.getAddTime());
				this.service.insertSysParameter(record);
				if(!JUtil.isNull(key) && !JUtil.isNull(value)){
					SystemUtil.getInstance().setParamMapValue(record.getParameterKey(), record.getParameterValue());
				}
				return setSuccess("添加成功!");
			} 
			SysParameter queryRecord = new SysParameter();
			queryRecord.setEnabled(CommonConstants.ENABLE);
			queryRecord.setId(record.getId());
			queryRecord = this.service.findSysParameter(queryRecord, null);
			if(JUtil.isNULL(queryRecord)){
				return setException("修改的记录不存在!修改ID="+record.getId());
			}
			record.setUpdateTime(new Date().getTime());
			this.service.modifySysParameterByPkSelective(record);
			if(!JUtil.isNull(key) && !JUtil.isNull(value)){
				if(!JUtil.isNull(queryRecord.getParameterKey())){
					SystemUtil.getInstance().removeParamMapByKey(queryRecord.getParameterKey());
					}
				SystemUtil.getInstance().setParamMapValue(key, value);
			}
			if(!JUtil.isNull(key) && JUtil.isNull(value)){
				if(!JUtil.isNull(queryRecord.getParameterKey())){
				SystemUtil.getInstance().removeParamMapByKey(queryRecord.getParameterKey());
				}
				SystemUtil.getInstance().setParamMapValue(key, queryRecord.getParameterValue());
			}
			if(JUtil.isNull(key) && !JUtil.isNull(value)){
				if(!JUtil.isNull(queryRecord.getParameterKey())){
					SystemUtil.getInstance().setParamMapValue(queryRecord.getParameterKey(), value);
				}
			}
			return setSuccess("修改成功!");
		}catch(Exception e){
			log.error(e.getMessage(), e);
			return setFail("处理异常!");
		}
	}
	
	/**
	 * 物理删除
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "drop")
	@ResponseBody
	public Object drop(@ModelAttribute final  RequestParameters requestParameters){
		try {
			if(StringUtils.isEmpty(requestParameters.getPks())){
				return setFail("删除ID为空!");
			}
			SysParameter queryRecord = new SysParameter();
			queryRecord.setEnabled(CommonConstants.ENABLE);
			Integer pk = JUtil.strToInt(requestParameters.getPks());
			queryRecord.setId(pk);
			queryRecord = this.service.findSysParameter(queryRecord, null);
			if(JUtil.isNULL(queryRecord)){
				return setException("删除I的记录不存在!删除IID="+pk);
			}
			service.dropSysParameter(requestParameters.getPks());
			SystemUtil.getInstance().removeParamMapByKey(queryRecord.getParameterKey());
			return setSuccess("success");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return setException("fail",e.getMessage());
		}
	}
	
	/**
	 * 逻辑删除
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(@ModelAttribute final  RequestParameters requestParameters){
		try {
			if(StringUtils.isEmpty(requestParameters.getPks())){
				return setFail("删除ID为空!");
			}
			SysParameter queryRecord = new SysParameter();
			queryRecord.setEnabled(CommonConstants.ENABLE);
			Integer pk = JUtil.strToInt(requestParameters.getPks());
			queryRecord.setId(pk);
			queryRecord = this.service.findSysParameter(queryRecord, null);
			if(JUtil.isNULL(queryRecord)){
				return setException("删除I的记录不存在!删除IID="+pk);
			}
			service.deleteSysParameter(requestParameters.getPks());
			SystemUtil.getInstance().removeParamMapByKey(queryRecord.getParameterKey());
			return setSuccess("success");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return setException("fail",e.getMessage());
		}
	}

}
